package com.example.testanimation.graphics.old;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;

public class MyAnimation  {
		int mSpectrumNum = 30;
		private float[] mPoints = null;
		private Paint paint = new Paint();

		private int horizontalSpace = 33;
		private int refeshIndex;
		
		private int animationIndex = 1;
		
		private int refreshIndexStep = 1;
		
		ArrayList<int[]> arrayAnimationData = null;
		private Context mContext;
		private float level = 1;
		private float mMaxLevel = 100;

		public MyAnimation(Context context) {
			mContext = context;
			arrayAnimationData = getArray();
			mPoints = new float[mSpectrumNum * 4];
		}

		private ArrayList<int[]> getArray() {
			ArrayList<int[]> array;
			AssetManager assetManager = mContext.getAssets();
			try {
				InputStream in = assetManager.open("data.txt");
				BufferedReader br = new BufferedReader(new InputStreamReader(in));
				String line = null;
				array = new ArrayList<int[]>();
				while ((line = br.readLine()) != null) {
					String[] value = line.split(":");
					int valueInt[] = new int[value.length];
					for (int i = 0; i < valueInt.length; i++) {
						valueInt[i] = Integer.parseInt(value[i]);
					}
					array.add(valueInt);
				}
				return array;

			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}

		public void setAnimationIndex(int index){
			animationIndex = index;
		}
		
		
		public void refresh(int startX , int centerY) {
			for (int i = 0; i < mSpectrumNum; i++) {
				float spectrmHeight =  (getHeight(i) * level);
				float startY = centerY - spectrmHeight / 2;
				float endY = centerY + spectrmHeight / 2;
				float x = startX + i * horizontalSpace;
				mPoints[i * 4] = x;
				mPoints[i * 4 + 1] = startY;
				mPoints[i * 4 + 2] = x;
				mPoints[i * 4 + 3] = endY;
			}
			
			refeshIndex+=refreshIndexStep;
		}

		final int noChangeEndIndex = 29;
		final int animationCount = 44;
		final int recognitionIndexStart = 172;
		final int recognitionIndexEnd = 223;
		
		final int animationEndIndex = noChangeEndIndex + animationCount;
		private int getHeight(int index) {
			System.out.println("refeshIndex is " + refeshIndex);
			switch(animationIndex){
			case 0:
				break;
			case 1:
				//动画,语音输入的
				if(refeshIndex > noChangeEndIndex && refeshIndex < animationEndIndex ){
					
				}else{
					refeshIndex = noChangeEndIndex ;
					refreshIndexStep = 1;
				}
				break;
			case 2:
				//这个你在识别
				if(refeshIndex > recognitionIndexStart && refeshIndex < recognitionIndexEnd ){
					
				}else{
					refeshIndex = recognitionIndexStart;
					refreshIndexStep = 1;
				}
				break;
			case 3:
				break;
			}
			
			int[] a = arrayAnimationData.get(refeshIndex % arrayAnimationData.size()); 
			return a[index];
		}

		public void setLevel(int level){
			if(level < 60){
				level = 60;
			}
			
			this.level = level / mMaxLevel;
		}
		
		public void setMaxLevel(float maxLevel){
			mMaxLevel = maxLevel;
		}
		
		public void draw(Canvas canvas) {
			paint.setStrokeWidth(10f);
			paint.setAntiAlias(true);
			paint.setColor(Color.parseColor("#2996ff"));
			canvas.save();
			canvas.drawLines(mPoints, paint);
			canvas.restore();
		}

		public void reset() {
			refeshIndex = 0;
		}

	}